package com.lgq.dict.serivce.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lgq.dict.mapper.DictMapper;
import com.lgq.dict.serivce.DictSerivce;
import com.lgq.dict.util.RedisUtil;
import com.lww.springcloudapi.entity.TabCode;
import com.lww.springcloudapi.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 字典表service实现层
 */
@Service
public class DictSericeimpl extends ServiceImpl<DictMapper, TabCode> implements DictSerivce {

    @Resource
    private DictMapper dictMapper;

    @Autowired
    private RedisUtil redisUtil;

    /*
    字典表查询可以进行字典编码和字典编码类型进行查询
     */
    @Override
    public Result dictParent(String parent) {
        QueryWrapper<TabCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent", parent);
        return Result.ok(dictMapper.selectList(queryWrapper));
    }

    /*
    根据字典编码类型进行更新
     */
    @Override
    public Result dictUpdate(TabCode tabCode) {
        int result = dictMapper.updateById(tabCode);
        return Result.ok(result);
    }

    /*
    根据字典编码类型删除
     */
    @Override
    public Result dictDelete(String type) {
        QueryWrapper<TabCode> queryWrapper = new QueryWrapper<>();
        return Result.ok(dictMapper.delete(queryWrapper));
    }

    @Override
    public Result formulaClothClassTree() {
        Object dict = redisUtil.get("dict");
        if (dict == null || dict == "") {
            redisUtil.set("dict", dict = dictMapper.getNodeTree(), 60, TimeUnit.MINUTES);
            System.err.println(dict);
            return Result.ok(dict);
        }
        System.err.println("+++++++++++++++====>" + dict);
        return Result.ok(dict);
    }

}
